package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import com.model.User;
import com.service.PaperService;
import com.service.UserService;
import com.util.BaseAction;

public class UserAction extends BaseAction {
    public void setFlag(String flag) {
        this.flag = flag;
    }

    private static final long serialVersionUID = -8686354798160293118L;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private User user;

    private String role;

    private String info;

    private UserService userService;

    private PaperService paperService;

    private String name;

    private String flag;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserService getUserService() {
        return userService;
    }

    @Resource(name = "UserService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public PaperService getPaperService() {
        return paperService;
    }

    @Resource(name = "PaperService")
    public void setPaperService(PaperService paperService) {
        this.paperService = paperService;
    }

    public String login() {

        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");

        List<User> list = null;
        if (user == null) {
            user = new User();
        }
        User u = new User();
        u.setUsername(user.getUsername());

        if (u.getUsername() == null) {
            return "login";
        }

        list = userService.findByExample(u);
        if (list.size() == 0) {
            info = "改用户还没注册！";
            return "login";
        } else {
            u.setUserpassword(user.getUserpassword());
            list = userService.findByExample(u);
            if (list.size() == 0) {
                info = "密码错误!";
                return "login";
            }
        }
        User loginUser = new User();
        loginUser = userService.findByExample(u).get(0);

//        User tmp = (User) session.get("loginUser");
//        if(null != tmp && tmp.getUsername().equals(loginUser.getUsername())){
//        	info = "重复登陆！";
//        	return "login" ;
//        }
        
        if (loginUser.getRole().equals("0")) {
            role = "0";// 学生
        } else if (loginUser.getRole().equals("1")) {
            role = "1";// 老师
        } else if (loginUser.getRole().equals("2")) {
            role = "2";// 管理员
        }
        session.put("loginUser", loginUser);// 登陆成功则把登陆者信息放到session中。
        session.put("role", role);
        String isclass = "no";
        session.put("isclass", isclass);

        return "index";

    }

    public String adduser() {
        user.setRole("0");
        if (userService.save(user)) {
            return "regsucc";
        } else {
            return "errorl";
        }
    }

    public void userexit() throws IOException {
        response.setContentType("text/html;charset=UTF-8.");
        response.setCharacterEncoding("utf-8");
        String displayname= new String(name.getBytes("ISO-8859-1"), "UTF-8");
        PrintWriter out = null;
        String username = displayname.trim();
        User u = new User();
        u.setUsername(username);
        List<User> list = userService.findByExample(u);
        System.out.println(list.size());
        System.out.println(list.get(0).getUsername());
        if (!list.isEmpty()) {
            out = response.getWriter();
            out.print("0%该用户已存在");
        } else {
            out = response.getWriter();
            out.print("1%可以注册");
        }
    }

    public String userInfo() {
        user = (User) session.get("loginUser");
        return "userinfo";
    }

    public String editInfo() {
        user = (User) session.get("loginUser");

        return "editinfo";
    }

    public String toInfo() {

        String password = user.getUserpassword().trim();
        String tel = user.getTel().trim();
        if (tel.equals("") || password.equals("")) {
            info = "密码和联系电话均不可为空";
            return "backEdit";
        } else {
            user = userService.editUser(user);
            info = "修改成功";
            if (flag != null && flag.equals("user")) {
                if (session.get("role").equals("stu")) {
                    return "stuEditok";
                } else {
                    return "teaEditok";
                }
            } else {
                session.put("loginUser", user);
                return "userinfo";
            }
        }
    }

    public String exit() {
        session.clear();
        return "exit";
    }

}
